package com.central.scheduler.dao.handler;

import com.central.scheduler.common.task.TaskTimeoutParameter;
import com.central.scheduler.spi.utils.JSONUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Tindy
 * @date 2021/6/23
 * @describe
 */
@MappedJdbcTypes(JdbcType.VARCHAR)  // 数据库类型
@MappedTypes({TaskTimeoutParameter.class})          // java数据类型
public class TaskTimeoutParamsToStringHandler extends BaseTypeHandler<TaskTimeoutParameter> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, TaskTimeoutParameter taskTimeoutParameter, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSONUtils.toJsonString(taskTimeoutParameter));
    }

    @Override
    public TaskTimeoutParameter getNullableResult(ResultSet resultSet, String s) throws SQLException {
        TaskTimeoutParameter taskTimeoutParameter = JSONUtils.parseObject(resultSet.getString(s), TaskTimeoutParameter.class);
        return taskTimeoutParameter;
    }

    @Override
    public TaskTimeoutParameter getNullableResult(ResultSet resultSet, int i) throws SQLException {
        TaskTimeoutParameter taskTimeoutParameter = JSONUtils.parseObject(resultSet.getString(i), TaskTimeoutParameter.class);
        return taskTimeoutParameter;
    }

    @Override
    public TaskTimeoutParameter getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        TaskTimeoutParameter taskTimeoutParameter = JSONUtils.parseObject(callableStatement.getString(i),TaskTimeoutParameter.class);
        return taskTimeoutParameter;
    }
}

